
package com.faithbj.device.web.controller.admin;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.faithbj.device.model.configuration.Pager;
import com.faithbj.device.model.entity.Cost;
import com.faithbj.device.service.CostService;
import com.faithbj.device.web.controller.BaseController;

/**
 * @author jrn E-mail: rinanjin@gmail.com
 * 
 * @version 0.1 
 * 
 * 创建时间：2012-5-16 下午08:16:35
 * 
 * 类说明
 */
@Controller
@RequestMapping(value="faith/cost")
public class AdminCostController extends BaseAdminController
{
	private static final long serialVersionUID = 1L;

	private final String OPERRATE_RETURN_URL = "/faith/cost/list";
	
	private final String LIST = "/admin/cost_list";
	
	private final String INPUT = "/admin/cost_input";
	
	@Resource(name="costService")
	private CostService costService;
	
	@RequestMapping(value = "/menu")
	public String menu()
	{
		return "/admin/menu_cost";
	}
	
	@RequestMapping(value = "/list")
	public String list(@ModelAttribute("cost") Cost cost, @ModelAttribute("pager") Pager pager, ModelMap map)
	{
		Pager pagerVal = costService.findByPager(pager, cost);
		map.put("pager", pagerVal);
		return LIST;
	}
	
	@RequestMapping(value = "/new")
	public String add(ModelMap map)
	{
		map.put("isAddAction", true);
		return INPUT;
	}
	
	@RequestMapping(value = "/edit")
	public String edit(@RequestParam("id") String id, ModelMap map)
	{
		Cost cost = costService.get(id);
		map.put("cost", cost);
		map.put("isAddAction", false);
		return INPUT;
	}
	
	@RequestMapping("/delete")
	public String delete(@RequestParam String id,ModelMap map)
	{
		costService.delete(id);
		map.put("redirectUrl", OPERRATE_RETURN_URL);
		return SUCCESS;
	}
	
	@RequestMapping("/batchDelete")
	@ResponseBody
	public Map<String, String> batchDelete(@RequestParam String[] ids)
	{
		costService.batchDelete(ids);
		Map<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("status", "success");
		resultMap.put("message", "批量删除操作成功");
		return resultMap;
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(@ModelAttribute("deviceInfo") Cost cost, ModelMap map)
	{
		costService.save(cost);
		map.put("redirectUrl", OPERRATE_RETURN_URL);
		return SUCCESS;
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(@ModelAttribute("deviceInfo") Cost cost, ModelMap map)
	{
		costService.update(cost);
		map.put("redirectUrl", OPERRATE_RETURN_URL);
		return SUCCESS;
	}

	@Resource(name="costService")
	public void setCostService(CostService costService)
	{
		this.costService = costService;
	}
}
